﻿using UnityEngine.Events;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Cinemachine;
using System;
public class CameraCutEffect : MonoBehaviour
{

    public CinemachineBrain cinemachineBrain;
    public UnityAction<CinemachineBrain> CutAction;
    /// <summary>
    /// 当前是否处于切换过程中
    /// </summary>
    private bool isCutting = false;

    public bool isNeedCutFade=false;
    void Start()
    {
        CutAction = CameraCutEffects;
        cinemachineBrain.m_CameraCutEvent.AddListener(CutAction);
    }


    void CameraCutEffects(CinemachineBrain cameraBrain)
    {
        isCutting = true;
        Fade(true);
    }
    //淡入时间
    public float fadeInTime = 2.0f;
    //淡出时间
    public float fadeOutTime = 2.0f;
    /// <summary>
    /// bool值为True代表屏幕由白变黑
    /// bool值为False代表屏幕由黑变白
    /// </summary>
    public Action<bool> blackCompleteAction;
    private Color fadeInColor = new Color(0f, 0.0f, 0.0f, 1.0f);
    private Color fadeOutColor = new Color(0.0f, 0.0f, 0.0f, 0.01f);
    private Material fadeMaterial = null;
    private bool isFadingIn = false;
    private bool isFadeOut = false;
    private bool isFade = false;
    private YieldInstruction fadeInstruction = new WaitForEndOfFrame();
    void Awake()
    {
        fadeMaterial = new Material(Shader.Find("Camera/CameraCutShader"));
    }
    void OnDisable()
    {
        isFadeOut = false;
        isFadingIn = false;
    }
    void OnDestroy()
    {
        if (fadeMaterial != null)
        {
            isFadeOut = false;
            isFadingIn = false;
            Destroy(fadeMaterial);
        }
    }
    private void OnPostRender()
    {
        if (isCutting == false) return;
        if (isFadingIn)
        {
            fadeMaterial.SetPass(0);
            GL.PushMatrix();
            GL.LoadOrtho();
            GL.Color(fadeMaterial.color);
            GL.Begin(GL.QUADS);
            GL.Vertex3(0f, 0f, -12f);
            GL.Vertex3(0f, 1f, -12f);
            GL.Vertex3(1f, 1f, -12f);
            GL.Vertex3(1f, 0f, -12f);
            GL.End();
            GL.PopMatrix();
        }
        if (isFadeOut)
        {
            fadeMaterial.SetPass(0);
            GL.PushMatrix();
            GL.LoadOrtho();
            GL.Color(fadeMaterial.color);
            GL.Begin(GL.QUADS);
            GL.Vertex3(0f, 0f, -12f);
            GL.Vertex3(0f, 1f, -12f);
            GL.Vertex3(1f, 1f, -12f);
            GL.Vertex3(1f, 0f, -12f);
            GL.End();
            GL.PopMatrix();
        }
    }
    IEnumerator FadeIEnumator(bool fadeIn)
    {
        float fadeTime = fadeIn ? fadeInTime : fadeOutTime;

        isFadingIn = fadeIn ? true : false;
        isFadeOut = fadeIn ? false : true;

        float elapsedTime = 0.0f;

        Color color = fadeIn ? fadeInColor : fadeOutColor;

        fadeMaterial.color = color;

        while (elapsedTime < fadeTime)
        {
            yield return new WaitForEndOfFrame();
            elapsedTime += Time.deltaTime;
            float a = fadeIn ? 1 - Mathf.Clamp01(elapsedTime / fadeInTime) : Mathf.Clamp01(elapsedTime / fadeOutTime);
            color.a = a;
            fadeMaterial.color = color;
        }
        isCutting = false;
        if (blackCompleteAction != null)
        {
            blackCompleteAction.Invoke(fadeIn);
        }
    }
    /// <summary>
    /// 淡入或者淡出
    /// </summary>
    /// <param name="isFadeIn">是否是淡入，true为淡入，false为淡出</param>
    public void Fade(bool isFadeIn)
    {
        if (!isNeedCutFade)return;
        StartCoroutine(FadeIEnumator(isFadeIn));
    }
    /// <summary>
    /// 直接淡入淡出
    /// </summary>
    public void Fade()
    {
         if (!isNeedCutFade)return;
        StartCoroutine(FadeInOutIenumator());
    }
    private IEnumerator FadeInOutIenumator()
    {
        yield return StartCoroutine(FadeIEnumator(true));
        yield return null;
        yield return StartCoroutine(FadeIEnumator(false));
    }
}
